package com.wolfking.converter.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.SecureRandom;

@SuppressWarnings("all")
public class CaesarUtil {


    public static String encode(String content) {
        return Caesar.encrypt(content);
    }

    public static String decode(String cipher) {
        return Caesar.decrypt(cipher);
    }

    public static String blur(String cipher) {
        String result = "";
        String plainText = decode(cipher);
        int middle;
        if (plainText.length() > 1 && plainText.length() <= 5) {
            middle = plainText.length() - 1;
            result = "*" + plainText.substring(middle);
        } else if (plainText.length() <= 10) {
            result = "**";
        } else if (plainText.length() == 11) {
            result = plainText.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        } else if (plainText.length() == 18) {
            result = plainText.replaceAll("(\\d{6})\\d{8}(\\d{4})", "$1********$2");
        } else {
            middle = plainText.length() - 8;
            result = plainText.replaceAll("(\\w{4})\\w{" + middle + "}(\\w{4})", "$1****$2");
        }

        return result;
    }

    public static boolean isEncrypted(String cipherText) {
        return Caesar.isEncrypted(cipherText);
    }


    private static class Caesar {
        private static Key KEY = null;
        private static String PLACEHOLDER = "@#WE2CN#@";

        public Caesar() {
        }

        public static Key getKey(String keySeed) {
            if (keySeed != null && keySeed.trim().length() != 0) {
                try {
                    SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
                    secureRandom.setSeed(keySeed.getBytes());
                    KeyGenerator generator = KeyGenerator.getInstance("AES");
                    generator.init(secureRandom);
                    return generator.generateKey();
                } catch (Exception var3) {
                    throw new RuntimeException(var3);
                }
            } else {
                throw new RuntimeException("Key can not be empty.");
            }
        }

        public static final String encrypt(String plainText) {
            plainText = PLACEHOLDER + plainText;
            Key secretKey = KEY;

            try {
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(1, secretKey);
                byte[] p = plainText.getBytes("UTF-8");
                byte[] result = cipher.doFinal(p);
                BASE64Encoder encoder = new BASE64Encoder();
                String encoded = encoder.encode(result);
                return encoded;
            } catch (Exception var7) {
                throw new RuntimeException("plainText may be wrong.", var7);
            }
        }

        public static final String decrypt(String cipherText) {
            Key secretKey = KEY;

            try {
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(2, secretKey);
                BASE64Decoder decoder = new BASE64Decoder();
                byte[] c = decoder.decodeBuffer(cipherText);
                byte[] result = cipher.doFinal(c);
                String plainText = new String(result, "UTF-8");
                plainText = plainText.replaceAll(PLACEHOLDER, "");
                return plainText;
            } catch (Exception var7) {
                throw new RuntimeException("", var7);
            }
        }

        public static boolean isEncrypted(String cipherText) {
            Key secretKey = KEY;

            try {
                Cipher cipher = Cipher.getInstance("AES");
                cipher.init(2, secretKey);
                BASE64Decoder decoder = new BASE64Decoder();
                byte[] c = decoder.decodeBuffer(cipherText);
                byte[] result = cipher.doFinal(c);
                String plainText = new String(result, "UTF-8");
                return plainText.contains(PLACEHOLDER);
            } catch (Exception var7) {
                return false;
            }
        }

        static {
            KEY = getKey("+9bozxsElq9tKD7xRthXyNbwYT0xDD+vNLfv9xMq9XE=");
        }
    }
}
